/* Implementation of hashing for checking the passwords corresponding to the user id. 
    Open addressing is used with linera probing on hash table of size 10.
*/
// LOKESH GARG (2010043)
// DIVIDING THE LENGTH OF STRING WITH SIZE OF HASH TABLE.   


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
struct node
{
	char username[30];
	char password[15];
	
}n[10];

void search()									// To look in hash table for existence of user name
{
	char uid[30],pswd[15];
	int i=0,x,j=0,sum=0;
	int hvalue,count=1,flag=1;
	printf("Enter the user id : ");						// taking input username from user
	scanf("%s",uid);
	x = strlen(uid);
	for(j=0;j<x;j++)
	{ 
		sum=sum+uid[j];							//finding the sum of ASCII values of each character in string to find hash value
	}
	while(i!=10&&strcmp(uid,"")!=0)
	{
		hvalue=(x+i)%10;
		if(strcmp(n[hvalue].username,uid)==0)				// finding the hashvalue using above described algo
		{
			flag = 0;
			break;							// terminating the loop if username matches
		}
		else
		{
			i++;							// incrementing the loop counter until the username matches 
			flag++;			
		}
	}
	if(flag == 0)
	{
		printf("Enter the password : ");				// taking password input if username matched
		scanf("%s",pswd);
		if(strcmp(n[hvalue].password,pswd)==0 && count<=3)		//checking the no. of attempts user made in entering the password
		{
			printf("\nPassword & user id is correct\n\n !! U are logged in !!\n\n **** cheers ****\n");
		}
		while(strcmp(n[hvalue].password,pswd)!=0 && count<3)		// loop to give 3 chance to user to enter correct password
		{
	//printf("password & user id is correct\n !! u r logged in !!\n **** cheers ****");
			printf("\n!! Wrong password !!\n ** TRY AGAIN **\n\n");
			printf("Enter the password : ");
			scanf("%s",pswd);			
			if(strcmp(n[hvalue].password,pswd)==0 && count<3)		
			{
				printf("\nPassword & user id is correct\n\n !! U are logged in !!\n\n **** cheers ****\n");
				break;
			}
			else
			{
				count++;
			}
		}
		if(strcmp(n[hvalue].password,pswd)!=0 && count >=3)
		{
			printf("\nNo more logins allowed \n");				// terminating if all attempts fail
		}	
	}
	else
	{
		printf(" \nNo such user id \n");					// terminating if username didnt match
		printf("\n!! GO BACK TO HOME , TRY AGAIN  LATER !!\n\n");
	}		
}

void main()
{
	int x,i,hvalue,sum=0,j=0;
	FILE *fp;
	char user[30];
	char pass[15];
	fp=fopen("username.txt","r");
	if(fp!=NULL)
	{
		while(fscanf(fp,"%s %s",user,pass)!=EOF)				// iterating on file to make hash table
		{
		
	//	printf("\n%d",sum);
			i=0;
			x = strlen(user);
			for(j=0;j<x;j++)
			{ 
	 			sum=sum+user[j];
			}
//			printf("\n%s %s",user,pass);
//			printf("\n%d",sum);
			while(i!=10)
			{
	   			hvalue=(x+i)%10;					//generating hash value to store username & password in table
//				printf("\n%s %s",user,pass);
//				printf("\n%d",hvalue);
				if(strcmp(n[hvalue].username,"")==0)
				{
					strcpy(n[hvalue].username,user);		// storing the username & password in hashtable
					strcpy(n[hvalue].password,pass);
					break;	
				}
				else
				{
					i++;
				}
			
			}
		}
	}
	else										// terminating if file doen't exists.
	{	
		printf("File doesn't exists.\n");
		exit(1);
	}
	fclose(fp);
	search();						//calling the search function to take input & find the authentication of passwrods & username
}
